import datetime, os

from django.shortcuts import render, HttpResponse

from apps.api.models import models


# Create your views here.


def login(request):
    print(1)
    return render(request, "api/login.html")


class Person(object):
    def __init__(self, name, age):
        self.name = name
        self.age = age


def index(request):
    print(1)
    context = {
        "name": "kobe",
        "age": 27,
        "n1": [1, 2, 3, 4],
        "n2": {"name": "东契奇", "age": 23, },
        "n3": Person("科比", 39),
        "n4": datetime.datetime.now()
    }
    return render(request, "api/index.html", context)


def testEnv(request):
    env_dist = os.environ  # environ是在os.py中定义的一个dict environ = {}
    for key in env_dist:
        print(key + ' : ' + env_dist[key])

    email = os.getenv('EMAIL')
    print(email)

    return HttpResponse("EMAIL env is :" + email)


def addUser(request):
    # 1. 新增
    # v1 = models.Goods.objects.create(title="电脑",detail="thinkbook 14+",price=5999.9,count=1)
    # print(v1)
    v2 = models.Goods.objects.create(**{"title": "电脑4", "detail": "thinkbook 16+", "price": 7999.9, "count": 1})
    print(v2)
    return HttpResponse("addUser")


def queryUser(request):
    # 1. 查单个
    v1 = models.Goods.objects.first()
    print(v1)
    # 2. 根据条件过滤查询
    v2 = models.Goods.objects.filter(id=1)
    print(v2.query)
    # 根据条件过滤查询2
    v2 = models.Goods.objects.filter(id__gt=1)
    print(v2.query)
    # 根据条件过滤查询3
    v3 = models.Goods.objects.filter(id__gte=1)
    print(v3.query)
    # 根据条件过滤查询4
    v4 = models.Goods.objects.filter(id__lte=3)
    print(v4.query)
    # 根据条件过滤查询5
    v5 = models.Goods.objects.filter(id__in=[1, 2, 3])
    print(v5.query)
    # 根据条件过滤查询6
    v6 = models.Goods.objects.filter(title__contains="电")
    print(v6.query)
    # 根据条件过滤查询7
    v7 = models.Goods.objects.filter(title__startswith="电")
    print(v7.query)
    # 根据条件过滤查询8
    v8 = models.Goods.objects.filter(title__isnull=True)
    print(v8.query)
    # 根据条件过滤查询9,放回对象数组列表，直接遍历即可
    v9 = models.Goods.objects.all()
    print(v9)
    print(v9.query)
    for obj in v9:
        print(obj.id, obj.title, obj.detail, obj.price)

    # 根据条件过滤查询10，返回dict 和 v9 有所区别
    #  <QuerySet [{'title': '电脑', 'detail': 'thinkbook 14+'}, {'title': '电脑', 'detail': 'thinkbook 16+'}]>
    v10 = models.Goods.objects.filter(id__gt=1).values("title", "detail", "price")
    print(v10)
    print(v10.query)
    for obj in v10:
        print(obj["title"], obj["detail"], obj["price"])
    # 11. 根据条件过滤查询11,排除不等于，注意是使用的exclude
    v11 = models.Goods.objects.exclude(id=1)
    print(v11.query)
    # 12. 根据条件过滤查询12,排序asc
    v12 = models.Goods.objects.filter(id__gt=0).order_by("price")
    print(v12.query)
    # 13. 根据条件过滤查询12,排序id desc,price asc
    v13 = models.Goods.objects.filter(id__gt=0).order_by("-id", "price")
    print(v13.query)

    return HttpResponse("queryUser")


def deleteUser(request):
    # 1. 删
    v1 = models.Goods.objects.filter(id=5).delete()
    print(v1)
    # 2. 删所有
    # v2 = models.Goods.objects.all().delete()
    # print(v2)
    return HttpResponse("deleteUser")


def editUser(request):
    # 1. 修改
    v1 = models.Goods.objects.all().update(title="联想电脑")
    print(v1)
    # 2. 修改
    # v2 = models.Goods.objects.filter(id=6).update(title="联想电脑")
    # print(v2)
    # 3. 修改
    # v3 = models.Goods.objects.filter(id=6).update(**{"title":"联想电脑2"})
    # print(v3)
    return HttpResponse("deleteUser")
